package 接口测试;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Base64;
import java.util.concurrent.CompletableFuture;

public class FamilyDoctorSignService {

    // AES加密解密方法
    public static String encryptAES(String data, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(key.substring(0, 16).getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] encrypted = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decryptAES(String encryptedData, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(key.substring(0, 16).getBytes());
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] decodedBytes = Base64.getDecoder().decode(encryptedData);
        byte[] decrypted = cipher.doFinal(decodedBytes);
        return new String(decrypted);
    }

    // MD5签名方法
    public static String encryptMD5Sign(String data) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(data.getBytes());
        return bytesToHex(messageDigest);
    }

    private static String bytesToHex(byte[] hash) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : hash) {
            String hex = Integer.toHexString(0xff & b);
            if(hex.length() == 1) hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    }

    // 模拟异步请求获取签约患者列表
    public CompletableFuture<String> getSignInfoListAsync(String requestData) {
        // 这里应该是一个真实的网络请求，此处仅用模拟数据代替
        return CompletableFuture.supplyAsync(() -> {
            try {
                // 假设返回的数据已经被加密
                String encryptedResponse = encryptAES(requestData, "your-secret-key");
                // 解密响应
                return decryptAES(encryptedResponse, "your-secret-key");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        });
    }

    public static void main(String[] args) {
        FamilyDoctorSignService service = new FamilyDoctorSignService();
        service.getSignInfoListAsync("{\"patientId\":\"123\"}")
                .thenAccept(System.out::println)
                .exceptionally(ex -> {
                    System.err.println("Failed to fetch sign info list: " + ex.getMessage());
                    return null;
                });
    }
}